//
//  UIViewController+ZZUtil.m
//  ZZKitDemo
//
//  Created by hfzhangzhang on 2018/8/8.
//  Copyright © 2018年 hfzhangzhang. All rights reserved.
//

#import "UIViewController+ZZUtil.h"
#import "DynamicTools.h"
#import "objc/runtime.h"

@implementation UIViewController (ZZUtil)

#ifdef DEBUG

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class aClass = [self class];
        Swizzle_(aClass, NSSelectorFromString(@"viewDidAppear:"), @selector(dym_viewDidAppear:));
        Swizzle_(aClass, NSSelectorFromString(@"dealloc"), @selector(dym_dealloc));
    });
}

#endif

- (void)dym_viewDidAppear:(BOOL)animated {
    NSLog(@"\n---didAppear vc --> %@---\n", [self class]);
    [self dym_viewDidAppear:animated];
}

- (void)dym_dealloc {
    NSLog(@"\n---dealloc vc --> %@---\n", [self class]);
    [self dym_dealloc];
}


@end


@implementation UIViewController (Alert)

- (void)showActionSheetWithTitle:(NSString *)title message:(NSString *)message handle:(void(^)(NSInteger buttonIndex))handle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    
    NSMutableArray *titles = [NSMutableArray arrayWithCapacity:10];
    va_list params;  //定义一个指向个数可变的参数列表指针；
    id argument;
    if (otherButtonTitles) {
        [titles addObject:otherButtonTitles];
        va_start(params, otherButtonTitles);
        while ((argument = va_arg(params, NSString *))) {
            [titles addObject:argument];
        }
        va_end(params);//释放列表指针
    }
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
        BOOL hasCancelButton = [self isExistString:cancelButtonTitle];
        for (int i = 0; i < [titles count]; i++) {
            [alertController addAction:[UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (handle) handle(hasCancelButton ? i + 1 : i);
            }]];
        }
        
        if (hasCancelButton) {
            [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                if (handle) handle(0);
            }]];
        }
        
        [self presentViewController:alertController animated:YES completion:nil];
    } else {
        
    }

}

- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message handle:(void(^)(NSInteger buttonIndex))handle cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    
    NSMutableArray *titles = [NSMutableArray arrayWithCapacity:10];
    va_list params;  //定义一个指向个数可变的参数列表指针；
    id argument;
    if (otherButtonTitles) {
        [titles addObject:otherButtonTitles];
        va_start(params, otherButtonTitles);
        while ((argument = va_arg(params, NSString *))) {
            [titles addObject:argument];
        }
        va_end(params);//释放列表指针
    }
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        BOOL hasCancelButton = [self isExistString:cancelButtonTitle];
        for (int i = 0; i < [titles count]; i++) {
            [alertController addAction:[UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (handle) handle(hasCancelButton ? i + 1 : i);
            }]];
        }
        
        if (hasCancelButton) {
            [alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                if (handle) handle(0);
            }]];
        }
        
        [self presentViewController:alertController animated:YES completion:nil];
    } else {

    }
}

- (BOOL)isExistString:(NSString *)string {
    if (![string isKindOfClass:[NSString class]] ||
        string == nil ||
        [string isEqualToString:@""] ||
        [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) {
        return NO;
    }
    return YES;
}

@end



